Lexical Closures for C++
نویسنده
چکیده
We describe an extension of the C++ programming language that allows the nesting of function definitions and provides lexical closures with dynamic lifetime. Our primary motivation for this extension is that it allows the programmer to define iterators for collection classes simply as member functions. Such iterators take function pointers or closures as arguments; providing lexical closures lets one express state (e.g. accumulators) naturally and easily. This technique is commonplace in programming languages like Scheme, T, or Smalltalk-80, and is probably the most concise and natural way to provide generic iteration constructs in object oriented programming languages. The ability to nest function definitions also encourages a modular programming style. We would like to extend the C++ language in this way without introducing new data types for closures and without affecting the efficiency of programs that do not use the feature. In order to achieve this, we propose that when a closure is created, a short segment of code is generated that loads the static chain pointer and jumps to the body of the function. A closure is a pointer to this short segment of code. This trick allows us to treat a closure the same way as a pointer to an ordinary C++ function that does not reference any non-local, non-global variables. We discuss issues of consistency with existing scoping rules, syntax, allocation strategies, portability, and efficiency. 1 What we would like to add... and why We would like to be able to nest function definitions in C++ programs. In this section, we will discuss a number of reasons why the ability to nest function definitions is desirable. Almost all other modern programming languages such as Scheme, T, Smalltalk-80, Common Lisp, Pascal, Modula-2, and Ada offer this feature. To illustrate the utility of this language feature, we have to agree on syntax. To indicate that a function is defined inside another function, we will simply move its definition inside that function but otherwise write it the same way we would at global level. For example, the following fragment of code defines function2(int) inside function1(int): function1(int x) { // ... function2(int y) { // ... } // ... } ∗Author’s address: MIT Artificial Intelligence Laboratory, Room 711, 545 Technology Square, Cambridge, MA 02139, USA. The author was supported by a fellowship from the Fairchild foundation.
منابع مشابه
Lightweight Lexical Closures for Legitimate Execution Stack Access
We propose a new language concept called “L-closures” for a running program to legitimately inspect/modify the contents of its execution stack. L-closures are lightweight lexical closures created by evaluating nested function definitions. A lexical closure can access the lexicallyscoped variables in the creation-time environment and indirect calls to it provide legitimate stack access. By using...
متن کاملCapsules and Closures
Capsules are a clean representation of the state of a computation in higher-order programming languages with effects. Their intent is to simplify and replace the notion of closure. They naturally provide support for functional and imperative features, including recursion and mutable bindings, and ensure lexical scoping without the use of closures, heaps, stacks or combinators. We present a comp...
متن کاملLecture Notes on Storage Management 15-312: Foundations of Programming Languages
In our discussion of mutable storage, a question was raised: if we allocate a new storage cell for each ref expression we encounter, when do we release these storage cells? As we will discover today, a similar question will be raised when we reconsider our implementation of pairs, lists, and closures, or generally any aggregate data structure. In designing the E machine, our goal was to describ...
متن کاملUsing Scoping Rules as a Distributed Coordination Language
Keywords: coordination languages, Scheme, distributed systems, lexical scoping, distributed objects. Essential to coordinating a distributed application is organizing and traversing the distributed application graph. Lexical scoping performs this function among modern high-level programming languages, but does not have the same distinction in distributed programming systems, where it competes w...
متن کاملComputing with Capsules
Capsules provide a clean algebraic representation of the state of a computation in higherorder functional and imperative languages. They play the same role as closures or heapor stack-allocated environments but are much simpler. A capsule is essentially a finite coalgebraic representation of a regular closed λ-coterm. One can give an operational semantics based on capsules for a higher-order pr...
متن کاملذخیره در منابع من
با ذخیره ی این منبع در منابع من، دسترسی به آن را برای استفاده های بعدی آسان تر کنید
عنوان ژورنال:
دوره شماره
صفحات -
تاریخ انتشار 1988